package com.yswee.framework.widget.switchtab;

import com.yswee.framework.R;
import com.yswee.framework.utils.UnitUtils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

public class SwitchDotBar extends View {
	protected int total;
	protected int index;
	protected Point[] points;
	protected Paint paint;
	// params
	protected int dotMargin = 6;
	protected int focusDotColor = 0;
	protected int focusDotRadius = 4;
	protected int unfocusDotColor = 0;
	protected int unfocusDotRadius = 4;
	
	public void setDotMargin(int dotMargin) {
		this.dotMargin = dotMargin;
	}
	public void setFocusDotColor(int focusDotColor) {
		this.focusDotColor = focusDotColor;
	}
	public void setFocusDotRadius(int focusDotRadius) {
		this.focusDotRadius = focusDotRadius;
	}
	public void setUnfocusDotColor(int unfocusDotColor) {
		this.unfocusDotColor = unfocusDotColor;
	}
	public void setUnfocusDotRadius(int unfocusDotRadius) {
		this.unfocusDotRadius = unfocusDotRadius;
	}

	public SwitchDotBar(Context context) {
		this(context, null);
	}
	
	public SwitchDotBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		focusDotColor = getResources().getColor(R.color.widget_switchdotbar_focus_fg);
		unfocusDotColor = getResources().getColor(R.color.widget_switchdotbar_unfocus_fg);

		paint = new TextPaint();
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);
		
		TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
				R.styleable.SwitchDotBar);
		
		dotMargin = (int) typedArray.getDimension(R.styleable.SwitchDotBar_SwitchDotBar_dotMargin, 
				UnitUtils.dip2pix(getContext(), dotMargin));
		focusDotColor = typedArray.getColor(R.styleable.SwitchDotBar_SwitchDotBar_focusDotColor, focusDotColor);
		focusDotRadius = (int) typedArray.getDimension(R.styleable.SwitchDotBar_SwitchDotBar_focusDotRadius, 
				UnitUtils.dip2pix(getContext(), focusDotRadius));
		unfocusDotColor = typedArray.getColor(R.styleable.SwitchDotBar_SwitchDotBar_unfocusDotColor, unfocusDotColor);
		unfocusDotRadius = (int) typedArray.getDimension(R.styleable.SwitchDotBar_SwitchDotBar_unfocusDotRadius, 
				UnitUtils.dip2pix(getContext(), unfocusDotRadius));
		
		typedArray.recycle();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int measuredHeight = measureHeight(heightMeasureSpec);
		int measuredWidth = measureWidth(widthMeasureSpec);
		setMeasuredDimension(measuredWidth, measuredHeight);
	}
	
	protected int measureHeight(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		int result = 0;
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			result = getMinimumHeight() + getPaddingTop() + getPaddingBottom();
		}
		return result;
	}

	protected int measureWidth(int measureSpec) {
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		int result = 0;
		if (specMode == MeasureSpec.EXACTLY) {
			result = specSize;
		} else {
			result = getMinimumWidth() + getPaddingLeft() + getPaddingRight();
		}
		return result;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// calculate point
		if(total < 1 || index < 0 || index >= total)
			return;
		initializeDotBar();
		// draw dot
		for(int i = 0; i < total; i++) {
			if(i == index) {
				paint.setColor(focusDotColor);
				canvas.drawCircle(points[i].x, points[i].y, focusDotRadius, paint);
			} else {
				paint.setColor(unfocusDotColor);
				canvas.drawCircle(points[i].x, points[i].y, unfocusDotRadius, paint);
			}
		}
	}
	
	protected void initializeDotBar() {
		if(points == null) {
			points = new Point[total];
			int viewwidth = getWidth();
			int viewheight = getHeight();
			int totalwidth = getMinimumWidth();
			int left = (viewwidth - totalwidth) / 2;
			for(int i = 0; i < total; i++) {
				int radius = (i == index ? focusDotRadius : unfocusDotRadius);
				points[i] = new Point();
				points[i].y = viewheight / 2;
				points[i].x = left + radius;
				left += radius * 2 + dotMargin;
			}
		}
	}
	
	public int getMinimumWidth() {
		if(total > 0) {
			return (total - 1) * unfocusDotRadius * 2 + 
				focusDotRadius * 2 + 
				(total - 1) * dotMargin;
		} else {
			return UnitUtils.dip2pix(getContext(), 200);
		}
	}
	
	public int getMinimumHeight() {
		if(total > 0) {
			return Math.max(unfocusDotRadius, focusDotRadius) * 2;
		} else {
			return UnitUtils.dip2pix(getContext(), 20);
		}
	}
	
	/**
	 * 
	 * @param total
	 * @param index from 0
	 */
	public void setDatas(int total, int index) {
		if(total == this.total) {
			if(index != this.index) {
				this.index = index;
				requestLayout();
				invalidate();
			}
		} else {
			this.total = total;
			this.index = index;
			points = null;
			requestLayout();
			invalidate();
		}
	}
}
